package com.hefan.live.dao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cat.tiger.util.CollectionUtils;
import com.hefan.live.bean.LiveLog;

@Repository
public class LiveMonitorDao {

	@Resource
	JdbcTemplate jdbcTemplate;
	
	public List<LiveLog> getCurrentlyOnLiveList() {
		String sql = "select ll.abnormal_end,ll.chat_room_id,ll.display_graph," +
                "ll.end_time,ll.false_ticket_count,ll.handle_status,ll.id,ll.live_length," +
                "ll.live_name,ll.live_url,ll.live_uuid,ll.location,ll.opt_user,ll.pull_url," +
                "ll.start_time,ll.ticket_count,ll.user_id,ll.valid_live_length,ll.watch_num" +
                " from live_log ll where end_time is null ";
		List<LiveLog> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<LiveLog>(LiveLog.class));
		return CollectionUtils.isNotEmpty(list) ? list : null;
	}

	public List getHighRiskLiveList(String userId, String nickName, String startTime, String endTime) {
		StringBuilder querySql = new StringBuilder("");
		querySql.append("select a.user_id,a.chat_room_id,a.live_name,DATE_FORMAT(a.start_time,'%Y-%m-%d %H:%s') start_time,"
				+ " DATE_FORMAT(a.start_time,'%Y-%m-%d') back_time, DATE_FORMAT(a.end_time,'%Y-%m-%d %H:%s') end_time,a.live_uuid,")
		.append(" a.live_img,a.location,c.user_type,c.nick_name")
		.append(" from  live_log a RIGHT JOIN live_room b on a.user_id=b.user_id and b.high_status=2")
		.append(" LEFT JOIN web_user c on a.user_id=c.user_id")
		.append(" where a.end_time is not null ");
		if(StringUtils.isNotBlank(userId)){
			querySql.append(" and a.user_id='"+userId+"'");
		}
		if(StringUtils.isNotBlank(nickName)){
			querySql.append(" and c.nick_name like '%"+nickName+"%'");
		}
		if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
			querySql.append(" and a.start_time BETWEEN '"+startTime+"' AND '"+endTime+"'");
//			.append(" or a.end_time BETWEEN '"+startTime+"' AND '"+endTime+"')");
		}
		querySql.append(" order by a.start_time");
		List list = jdbcTemplate.queryForList(querySql.toString());
		return CollectionUtils.isNotEmpty(list) ? list : null;
	}
}
